package com.speljohan.rsbot.gui;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.StringTokenizer;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.script.ScriptHandler;

/**
 * Simple dialog utility to allow the user to easily add a script to the scripts
 * folder. This will automatically detect the class name, compile it, and reload
 * it into RSBot for the user.
 * 
 * @author blakeman8192
 */
public class AddScript extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1467221676974116606L;
	private final JScrollPane scriptPane;
	private final JTextArea scriptArea;
	private final JLabel scriptCodeLabel;
	private final JButton saveButton;

	private static final Logger log = Logger.getLogger(AddScript.class
			.getName());

	public AddScript(final Frame f) {
		super(f, "Add Script", true);
		scriptArea = new JTextArea(5, 5);
		scriptPane = new JScrollPane(scriptArea);
		scriptCodeLabel = new JLabel("Script code:");
		saveButton = new JButton("Add Script");
		setPreferredSize(new Dimension(623, 409));
		setLayout(null);
		scriptArea.setTabSize(4);
		scriptArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		scriptPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		add(scriptPane);
		add(scriptCodeLabel);
		add(saveButton);
		scriptPane.setBounds(10, 35, 600, 335);
		scriptCodeLabel.setBounds(10, 5, 100, 25);
		saveButton.setBounds(510, 5, 100, 25);
		saveButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(final MouseEvent e) {
				saveScript();
			}
		});
	}

	/**
	 * Saves the pasted script.
	 */
	private void saveScript() {
		final ScriptHandler sh = Bot.getScriptHandler();
		for (int i = 0; i < scriptArea.getLineCount(); i++) {
			try {
				final int lineStart = scriptArea.getLineStartOffset(i);
				final int lineEnd = scriptArea.getLineEndOffset(i);
				final String line = scriptArea.getText().substring(lineStart,
						lineEnd);
				if (line.contains("public class")) {
					final StringTokenizer st = new StringTokenizer(line, " ");
					st.nextToken();
					st.nextToken();
					final String className = st.nextToken();
					final File file = new File("./Scripts/" + className
							+ ".java");
					if (file.createNewFile()) {
						System.out.println("File created!");
						final BufferedWriter writer = new BufferedWriter(
								new FileWriter(file));
						writer.write(scriptArea.getText());
						writer.flush();
						writer.close();
						BotGUI.compile(true);
						sh.reload();
						JOptionPane.showMessageDialog(this,
								"Script successfully created.");
					} else {
						JOptionPane.showMessageDialog(this,
								"Script already exists.");
					}
					return;
				}
			} catch (final Exception ex) {
				log.warning("Error saving script");
				ex.printStackTrace();
			}
		}
		JOptionPane.showMessageDialog(this, "Specified text is not a script.");
	}

}
